<?php
require_once 'DB.class.php';
class Marker
{
    private $id;
    private $latitude;
    private $longitude;
    private $name;
    private $address;
    private $description;

    public static function find($params)
    {
        $sql = "
            select id, name, description, address, longitude, latitude, bookable
            from business
            where longitude is not null
            and latitude is not null
            and address is not null
        ";
        $where = array();
        if (array_key_exists('city_id', $params)) {
            $where[':city_id'] = $params['city_id'];
            $sql .= " and city_id = :city_id ";
        }

        if (array_key_exists('name', $params)) {
            $where[':name'] = "%".strtolower($params['name'])."%";
            $sql .= " and name like :name ";
        }

        $sql .= " order by business.name ";

        $db = DB::getInstance();
        $stmt = $db->prepare($sql);

        if (!$stmt instanceof PDOStatement) {
            throw new Excepton("Cannot prepare the query");
        }
        if (!$stmt->execute($where)) {
            throw new Exception("Cannot execute the query: " . print_r($stmt->errorInfo(), true));
        }
        $markers = array();
        while ($row = $stmt->fetch())
        {
            $marker = self::fromArray($row);
            $markers[] = $marker;
        }
        return $markers;
    }

    public static function fromArray($array)
    {
        $marker = new Marker();
        $marker->setId($array['id']);
        $marker->setLongitude($array['longitude']);
        $marker->setLatitude($array['latitude']);
        $marker->setName($array['name']);
        $marker->setAddress($array['address']);
        $marker->setDescription($array['description']);
        $marker->setBookable($array['bookable']);
        return $marker;
    }

    public function getLatitude()
    {
        return $this->latitude;
    }

    public function getLongitude()
    {
        return $this->longitude;
    }

    public function getName()
    {
        return $this->name;
    }

    public function getAddress()
    {
        return $this->address;
    }
    public function getBookable()
    {
        return $this->bookable;
    }

    public function getId()
    {
        return $this->id;
    }
    public function setLatitude($latitude)
    {
        $this->latitude = $latitude;
    }

    public function setLongitude($longitude)
    {
        $this->longitude = $longitude;
    }

    public function setName($name)
    {
        $this->name = $name;
    }

    public function setAddress($address)
    {
        $this->address = $address;
    }

    public function getDescription()
    {
        return $this->description;
    }

    public function setDescription($description)
    {
        $this->description = $description;
    }
    public function setBookable($bookable)
    {
        $this->bookable = $bookable;
    }
    public function setId($id)
    {
        $this->id = $id;
    }

}
